<template>
  <!-- 患者信息---就诊 ---唐莉 -->
  <el-dialog
    title="记录记录"
    :visible="value"
    width="700px"
    custom-class="dialog-box"
    @close="onClose"
    @open="onOpen"
    :close-on-click-modal="false"
    :append-to-body="true"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      @submit.native.prevent="onSubmit"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="记录时间">
            <el-date-picker
              v-model="form.date"
              type="date"
              :disabled="true"
              value-format="yyyy-MM-dd"
              placeholder="选择日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="血糖类型">
            <el-select v-model="form.type">
              <el-option v-for="(item,index) in timeList" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="血糖">
            <el-input v-model="form.value">
              <template slot="append">mmol/L</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="描述">
            <el-input v-model="form.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-button v-show="false" :loading="btnLoading" native-type="submit">提交</el-button>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "curItem", "type", "patientId", "typeLable", "caseType"],
  data() {
    return {
      form: {
        value: "",
        date: "",
        type: "",
        remark: "",
        signId: "", //体征详情id
        patientId: "",
        weeHours:'',//凌晨
        limosis:'',//空腹
        beforeSleep:'',//睡前
        afterDinner:'',//晚餐后
        beforeDinner:'',//晚餐前
        afterLunch:'',//午餐后
        antePrandium:"",//午餐前
        afterBreakfast:'',//早餐后
        beforeBreakfast:'',//早餐前
        bloodsugar: "" //血糖
      },
      timeList: [
        {
          id: 1,
          name: "凌晨"
        },
        {
          id: 2,
          name: "空腹"
        },
        {
          id: 3,
          name: "早餐前"
        },
        {
          id: 4,
          name: "早餐后"
        },
        {
          id: 5,
          name: "午餐前"
        },
        {
          id: 6,
          name: "午餐后"
        },
        {
          id: 7,
          name: "晚餐前"
        },
        {
          id: 8,
          name: "晚餐后"
        },
        {
          id: 9,
          name: "睡前"
        }
      ],
      rules: {},
      btnLoading: false
    };
  },
  created() {
    let th = this;
    th.form.date = th.getNewDate();
  },
  methods: {
    onClose() {
      let th = this;
      th.$refs["form"].resetFields();
      th.$emit("input", false);
    },
    onOpen() {},
    onSubmit() {
      let th = this;
      let j = {};
      switch(th.form.type){
        case 1:
          th.form.weeHours=th.form.value
          break;
        case 2:
          th.form.limosis=th.form.value
          break;
        case 3:
          th.form.beforeBreakfast=th.form.value
          break;
        case 4:
          th.form.afterBreakfast=th.form.value
          break;
        case 5:
          th.form.antePrandium=th.form.value
          break;
        case 6:
          th.form.afterLunch=th.form.value
          break;
        case 7:
          th.form.beforeDinner=th.form.value
          break;
        case 8:
          th.form.afterDinner=th.form.value
          break; 
        case 9:
          th.form.beforeSleep=th.form.value
          break;          
      }
      th.btnLoading = true;
      th.form.patientId = th.patientId;
      Object.assign(j, th.form);
      th.$api.savePatientGlucoseRecord(j).then(res => {
          if (res.executed) {
            th.$message.success("保存成功");
            th.$emit("success")
            th.onClose();
            th.btnLoading = false;
          } else {
            th.$message.error(res.message);
            th.onClose();
            th.btnLoading = false;
          }
        })
        .catch(error => {
          th.btnLoading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.btn_sign_option {
  padding: 10px;
}
</style>